<script setup>
import { onMounted, reactive, ref, computed } from "vue";
import Swiper from "swiper";
import "swiper/swiper-bundle.css";
import { useRoute } from "vue-router";
import { getCinemaDeatilByid, getCinemaonshowmovielist } from "../api/home";

const route = useRoute();
const id = route.query.cinemaId;
const activeIndex = ref(0);
const timeSelect = ref(0);
const isTimeSelect = ref(0);
const isTime = (id) => {
  timeSelect.value = id
  isTimeSelect.value = id
};
const data = reactive({
  cinemadetail: null,
  moviedetail: null,
  plist: []
});
onMounted(async () => {
  try {
    const [cinemaDetailRes, onShowMovieListRes] = await Promise.all([
      getCinemaDeatilByid(id),
      getCinemaonshowmovielist(id),
    ]);

    console.log("当前影院详情", cinemaDetailRes);
    data.cinemadetail = cinemaDetailRes.data.data;
    console.log("当前影院热映列表", onShowMovieListRes);
    console.log("当前热映内容", onShowMovieListRes.data.data.movies);
    data.moviedetail = onShowMovieListRes.data.data;
    data.plist = onShowMovieListRes.data.data.movies;



    const mySwiper = new Swiper(".swiper-container", {  //使用swiper前要先全局注册
      slidesPerView: "auto",
      centeredSlides: true,
      spaceBetween: 10,
      on: {
        slideChange() {
          activeIndex.value = this.activeIndex;
        },
      }
    });
    activeIndex.value = 0; // 设置默认值为0
  } catch (error) {
    console.error(error);
  }
});

const activeObject = computed(() => {
  if (data.plist && data.plist.length > activeIndex.value) {
    return [data.plist[activeIndex.value]]; // 将当前活动对象放入数组中
  }
  return [];
});

</script>

<template>
  <div>
    <div class="header">
      <nav>
        <router-link to="/movie">
          <img src="../assets/images/返回.png" width="22px" height="22px" class="goback" />
        </router-link>
        <h1 class="cinemaTitle">{{ data.cinemadetail?.nm }}</h1>
        <!-- 这个？用于防止查询不到的时候抛出错误 -->
        <div>
          <img src="https://p0.meituan.net/scarlett/f05f61e7a8f3f45fd071c068d7a26870356.png" />
        </div>
      </nav>
    </div>
    <div class="cinema-info">
      <h1 class="title">{{ data.cinemadetail?.nm }}</h1>
      <p class="line-ellipsis">{{ data.cinemadetail?.addr }}</p>
      <div class="location-icon border-1px-1">
        <img src="../assets/images/wxb定位.png" />
      </div>
    </div>
    <div class="movie-block">
      <div class="post-bg-filter">
        <div class="swiper-container movie-swiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="(movie, index) in data.moviedetail?.movies || []" :key="index"
              :class="{ active: index === activeIndex }" @click="activeIndex = index">
              <img :src="movie.img" style="object-fit: cover;  height: 98px;" />
            </div>
          </div>
        </div>
      </div>
      <div class="movies-info">
        <!-- 内容根据选中的图片索引进行展示 -->
        <div v-if="data.moviedetail?.movies.length">
          <div class="tiandsc">
            <h1 class="title">
              {{ data.moviedetail.movies[activeIndex]?.nm }}
            </h1>
            <p class="content">
              {{ data.moviedetail.movies[activeIndex]?.sc }}
            </p>
          </div>
          <p class="mes">{{ data.moviedetail.movies[activeIndex]?.desc }}</p>
        </div>
      </div>
    </div>
    <div class="show-warp">
      <div v-for="item in activeObject" :key="item.id">
        <div class="timeList">
          <div class="data-item" v-for="(e, id) in item.shows">
            <div class="date" :class="timeSelect == id ? 'active' : ''" @click="isTime(id)">{{ e.showDate }}
            </div>
          </div>
        </div>

        <div class="tips">
          <a href="" class="label">折扣</a>
          <a href="" class="ac">开卡享优惠</a>
          <a href="" class="acprice">9.9元起开卡 ></a>
        </div>

        <div
          v-if="item && item.shows && isTimeSelect !== undefined && item.shows[isTimeSelect] && item.shows[isTimeSelect].plist">
          <!-- 先判断是否有选中的时间，再判断是否有票，再判断是否有票列表，然后再循环渲染-->
          <div class="time-list" v-for="(e, id) in item.shows[isTimeSelect].plist">
            <div class="time-block">
              <div class="begin">{{ e.tm }}</div>
              <div class="end">散场时间自己算</div>
            </div>
            <div class="info-block">
              <div class="lang">{{ e.lang }} {{ e.tp }}</div>
              <div class="hall">{{ e.th }}</div>
            </div>
            <div class="price">
              <span>￥</span>47.5
              <div class="label">
                <span class="vipname">{{ e.vipPriceName }}</span>
                <span class="vipprice">￥{{ e.vipPrice }}</span>
              </div>
            </div>
            <div class="btn-block">
              <div class="btn">购票</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  max-width: 375px;
}

.a1 {
  font-family: 'mtsi-font';
}

.goback {
  //返回
  width: 30px;
  height: 30px;
}

.header {
  //头部
  background: #e54847;
  height: 50px;
  width: 100%;
  font-family: PingFangSC-Regular, "Hiragino Sans GB", sans-serif, Helvetica,
    宋体;

  nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    width: 95%;
    margin: 0 auto;

    h1 {
      font-size: 18px;
      color: white;
    }

    div {
      width: 17px;
      height: 16px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.cinema-info {
  //影院信息
  position: relative;
  padding: 15px 100px 15px 15px;

  .title {
    font-size: 17px;
    line-height: 24px;
    font-weight: 700;
    color: #333;
  }

  .line-ellipsis {
    margin-top: 2px;
    font-size: 13px;
    line-height: 18.5px;
    color: #999;
  }

  .location-icon {
    position: absolute;
    right: 0;
    top: 50%;
    width: 70px;
    height: 31px;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
    text-align: center;

    img {
      display: inline-block;
      width: 19px;
      height: 22px;
      margin-top: 5px;
    }
  }

  .border-1px-1:after {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 1px;
    content: "";
    background: #d8d8d8;
    -webkit-transform: scaleX(0.5);
    transform: scaleX(0.5);
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
  }
}

.movie-block {
  //电影滑动部分
  width: 100%;
  height: 250px;
  overflow: hidden;
  //position: absolute;

  .post-bg-filter {
    margin: 2px auto;
    position: relative;
    width: 100%;
    height: 140px;
    top: 0;
    left: 0;
    background-color: #40454d;
    padding-top: 15px;

    .swiper-container .movie-swiper {
      width: 100%;
      max-width: 100%;
      overflow: hidden;
    }

    .swiper-wrapper {
      display: flex;
    }

    .swiper-slide {
      flex-shrink: 0;
      width: auto; //根据图片大小自动给合适的宽度
      height: 100%;
      position: relative;
      transition-property: transform;
      display: block;
      margin-top: 5px;
    }

    .swiper-slide.active {
      -webkit-transform: scale(1.15);
      transform: scale(1.15);
      border: 2px solid #fff;
      box-sizing: border-box;
      padding: 0;
      margin-right: 0;
    }

    .swiper-slide:last-child {
      margin-right: 0;
    }
  }
}

.movies-info {
  padding: 11px 15px;
  text-align: center;

  .tiandsc {
    line-height: 24px;
    font-size: 17px;
    color: #333;
    font-weight: 700;

    .title {
      margin-right: 5px;
    }

    .content {
      color: #ffb400;
      font-size: 16px;
    }
  }

  .mes {
    margin-top: 5px;
    height: 18.5px;
    line-height: 18.5px;
    font-size: 13px;
    color: #999;
  }
}

.show-warp {
  .timeList {
    width: 375px;
    height: 45px;
    padding: 0 10px;
    margin-top: 10px;
    box-sizing: border-box;
    border-top: 1px solid #f0f0f0;

    .data-item {
      width: 88px;
      height: 45px;
      font-size: 14px;
      color: #666;
      font-weight: 100;
      line-height: 50px;
      float: left;
      text-align: center;

      .active {
        color: #f03d37;
        border-bottom: 2px solid #f03d37;
      }

      .date {
        height: 45px;
      }
    }
  }

  .tips {
    width: 375px;
    height: 42px;
    padding: 0 15px;
    display: flex;
    justify-content: space-between;
    background-color: #fff5ea;
    box-sizing: border-box;
    align-items: center;

    .label {
      width: 35px;
      height: 15px;
      font-size: 10px;
      color: #fff;
      line-height: 42px;
      background-color: #ff941a;
      border-radius: 3px;
      line-height: 15px;
      text-align: center;
    }

    .ac {
      width: 228px;
      height: 42px;
      color: #fa9600;
      font-size: 12px;
      line-height: 42px;
      margin-left: 10px;
    }

    .acprice {
      width: 90px;
      font-size: 12px;
      color: #999;
      line-height: 42px;
    }
  }

  .time-list {
    width: 375px;
    height: 86px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    padding: 17px 12px;
    border-top: 1px solid #f0f0f0;

    .time-block {
      width: 52px;

      .begin {
        font-size: 20px;
        font-weight: 400;
        color: #333;
      }

      .end {
        font-size: 11px;
        color: #999;
        margin-top: 5px;
        line-height: 1.2;
        margin-left: 2px;
      }
    }

    .info-block {
      margin-right: 10px;
      width: 100px;
      height: 52px;
      margin-left: 10px;

      .lang {
        font-size: 13px;
        color: #333;
      }

      .hall {
        font-size: 11px;
        color: #999;
        margin-top: 5px;
        line-height: 1.2;
      }
    }

    .price {
      width: 130px;
      height: 52px;
      font-size: 19px;
      color: #f03d37;

      span {
        font-size: 11px;
      }

      .label {
        width: 65px;
        height: 12px;
        display: flex;
        float: right;
        margin-right: 10px;
        margin-top: 2px;
        border-radius: 2px;
        align-items: center;
        overflow: hidden;
        border: 1px solid #ff9900;

        .vipname,
        .vipprice {
          font-size: 8px;
          padding: 2px;
        }

        .vipname {
          color: #fff;
          background-color: #ff9900;
        }

        .vipprice {
          color: #ff9900;
        }
      }
    }

    .btn-block {
      .btn {
        width: 47px;
        height: 27px;
        font-size: 12px;
        color: #f03d37;
        border: 1px solid #f03d37;
        border-radius: 12px;
        text-align: center;
        line-height: 27px;
      }
    }
  }
}
</style>
